/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.custom.categorylist;

import com.oushangfeng.pinneddemo.datainterface.CategoryItemBase;

import ohos.app.Context;

/**
 * CategoryItemFactory
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 * A factory to produce category item.
 */
public class CategoryItemFactory {
    /**
     * The total count of item type
     */
    public static final int TOTAL_ITEM_TYPE = 3;
    private static final int INT_2 = 2;

    /**
     * The method to create a list item of a specify category
     *
     * @param context current context
     * @param componentType the component type of list item
     * @param obj list item content
     * @param isHead list item content
     * @return category item
     */
    public static CategoryItemBase createListItem(Context context, boolean isHead, int componentType, String... obj) {
        CategoryItemBase item = null;
        switch (componentType) {
            case SingleListItem.ITEM_TYPE:
                if (obj == null || obj.length < INT_2) {
                    break;
                }
                item = new SingleListItem(context, obj[0], obj[1]);
                break;
            case SearchItem.ITEM_TYPE:
                item = new SearchItem(context);
                break;
            case HeadItem.ITEM_TYPE:
                if (obj == null || obj.length < 1) {
                    break;
                }
                item = new HeadItem(context, obj[0]);
                break;
            case SmallLabelItem.ITEM_TYPE:
                if (obj == null || obj.length < 1) {
                    break;
                }
                String[] split = obj[INT_2].split(",");
                if (isHead) {
                    item = new SmallLabelItem(context, HeadItem.ITEM_TYPE, obj[0], obj[1], split[0], split[1]);
                } else {
                    item = new SmallLabelItem(context, SearchItem.ITEM_TYPE, obj[0], obj[1], split[0], split[1]);
                }
                break;
            case StockHeadItem.ITEM_TYPE:
                if (obj == null || obj.length < 1) {
                    break;
                }
                item = new StockHeadItem(context, obj[0], HeadItem.ITEM_TYPE);

                break;
            case StockListItem.ITEM_TYPE:
                if (obj == null || obj.length < 1) {
                    break;
                }
                item = new StockListItem(context, obj[0]);
                break;
            case StockTitleItem.ITEM_TYPE:
                if (obj == null || obj.length < 1) {
                    break;
                }
                item = new StockTitleItem(context, obj[0], StockTitleItem.ITEM_TYPE);
                break;
            default:
                break;
        }
        return item;
    }
}
